﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Entity;
using System.Data.SqlClient;

namespace LotteryVote.Data
{
    public class GamePlayWayDataAccess : DataBase
    {
        private readonly string _baseSql = @"select gpw.*,gt.GameName,gt.CreateTime as GameType_CreateTime,
gt.Description as GameType_Description,pw.WayName,pw.CreateTime as PlayWay_CreateTime,
pw.Description as PlayWay_Description from tb_GamePlayWay as gpw
join tb_GameType gt on gt.GameId=gpw.GameId
left join tb_PlayWay pw on pw.WayId=gpw.WayId";

        public void InsertGamePlayWay(GamePlayWay gpy)
        {
            string sql = string.Format(@"Insert into {0} ({1},{2}) VALUES (@{1},@{2})  SELECT IDENT_CURRENT('{0}')",
                GamePlayWay.TABLENAME, GamePlayWay.GAMEID, GamePlayWay.WAYID);
            object id = base.ExecuteScalar(sql, new SqlParameter(GamePlayWay.GAMEID, gpy.GameType.GameId),
                new SqlParameter(GamePlayWay.WAYID, gpy.PlayWay.WayId));
            gpy.Id = Convert.ToInt32(id);
        }
        public void DeleteGamePlayWay(int gameId, int wayId)
        {
            string sql = string.Format(@"Delete {0} WHERE {1}=@{1} and {2}=@{2}", GamePlayWay.TABLENAME, GamePlayWay.GAMEID, GamePlayWay.WAYID);
            base.ExecuteNonQuery(sql, new SqlParameter(GamePlayWay.GAMEID, gameId), new SqlParameter(GamePlayWay.WAYID, wayId));
        }
        public void DeleteGamePlayWay(int id)
        {
            string sql = string.Format(@"DELETE {0} WHERE {1}=@{1}", GamePlayWay.TABLENAME, GamePlayWay.ID);
            base.ExecuteNonQuery(sql, new SqlParameter(GamePlayWay.ID, id));
        }

        public void InsertCompanyTypeSupportWayOfPlay(CompanyTypeSupportWayOfPlay wayOfPlay)
        {
            string sql = string.Format(@"INSERT INTO {0} ({1},{2}) VALUES(@{1},@{2})", CompanyTypeSupportWayOfPlay.TABLENAME,
                CompanyTypeSupportWayOfPlay.COMPANYTYPEID, CompanyTypeSupportWayOfPlay.GAMEPLAYWAYID);
            base.ExecuteNonQuery(sql, new SqlParameter(CompanyTypeSupportWayOfPlay.COMPANYTYPEID, wayOfPlay.CompanyType.Id),
                new SqlParameter(CompanyTypeSupportWayOfPlay.GAMEPLAYWAYID, wayOfPlay.GamePlayWay.Id));
        }
        public void DeleteCompanyTypeSupportWayOfPlay(int companyTypeId, int gamePlayWayId)
        {
            string sql = string.Format(@"DELETE {0} WHERE {1}=@{1} AND {2}=@{2}", CompanyTypeSupportWayOfPlay.TABLENAME,
                CompanyTypeSupportWayOfPlay.COMPANYTYPEID, CompanyTypeSupportWayOfPlay.GAMEPLAYWAYID);
            base.ExecuteNonQuery(sql, new SqlParameter(CompanyTypeSupportWayOfPlay.COMPANYTYPEID, companyTypeId),
                new SqlParameter(CompanyTypeSupportWayOfPlay.GAMEPLAYWAYID, gamePlayWayId));
        }

        public bool IsExistGamePlayWay(int gameId, int wayId)
        {
            string sql = string.Format(@"SELECT * FROM {0} WHERE {1}=@{1} and {2}=@{2}", GamePlayWay.TABLENAME, GamePlayWay.GAMEID, GamePlayWay.WAYID);
            var ds = base.ExecuteDataSet(sql, new SqlParameter(GamePlayWay.GAMEID, gameId), new SqlParameter(GamePlayWay.WAYID, wayId));
            return ds.Tables[0].Rows.Count > 0;
        }
        public bool IsCompanyTypeSupportWayOfPlay(int companyTypeId, int gamePlayWayId)
        {
            string sql = string.Format(@"SELECT COUNT(0) FROM {0} WHERE {1}=@{1} AND {2}=@{2}", CompanyTypeSupportWayOfPlay.TABLENAME,
                CompanyTypeSupportWayOfPlay.COMPANYTYPEID, CompanyTypeSupportWayOfPlay.GAMEPLAYWAYID);
            object count = base.ExecuteScalar(sql, new SqlParameter(CompanyTypeSupportWayOfPlay.COMPANYTYPEID, companyTypeId),
                new SqlParameter(CompanyTypeSupportWayOfPlay.GAMEPLAYWAYID, gamePlayWayId));
            return Convert.ToInt32(count) > 0;
        }

        public GamePlayWay GetGamePlayWay(int gamePlayWayId)
        {
            string sql = string.Format(@"{0} WHERE gpw.{1}=@{1}", _baseSql, GamePlayWay.ID);
            return base.ExecuteEntity<GamePlayWay>(sql, new SqlParameter(GamePlayWay.ID, gamePlayWayId));
        }
        public GamePlayWay GetGamePlayWay(int gameId, int wayId)
        {
            string sql = string.Format(@"{0} WHERE gt.{1}=@{1} and pw.{2}=@{2}", _baseSql, GameType.GAMEID, PlayWay.WAYID);
            return base.ExecuteEntity<GamePlayWay>(sql, new SqlParameter(GameType.GAMEID, gameId), new SqlParameter(PlayWay.WAYID, wayId));
        }
        public IList<GamePlayWay> GetAllGamePlayWay()
        {
            return base.ExecuteList<GamePlayWay>(_baseSql);
        }
        public IList<GamePlayWay> GetGamePlayWayByGameType(GameTypeEnum gameType)
        {
            string sql = string.Format(@"{0} where gt.{1}=@{1}", _baseSql, GameType.GAMEID);
            return base.ExecuteList<GamePlayWay>(sql, new SqlParameter(GameType.GAMEID, (int)gameType));
        }
        public IList<GamePlayWay> GetGamePlayWayByCompanyType(CompanyTypeModel companyType)
        {
            string sql = string.Format(@"{0} join tb_CompanyTypeSupportWayOfPlay csw on csw.GamePlayWayId=gpw.Id WHERE csw.{1}=@{1}",
                _baseSql, CompanyTypeSupportWayOfPlay.COMPANYTYPEID, companyType.Id);
            return base.ExecuteList<GamePlayWay>(sql, new SqlParameter(CompanyTypeSupportWayOfPlay.COMPANYTYPEID, companyType.Id));
        }
    }
}
